import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);
import {
	getphoneModel
} from '@/api/user.js'
import {
	getTrolleyList,
	addTrolley,
	getAddressList,
	getAreaOptions
} from '@/api/product.js'
const store = new Vuex.Store({
	state: {
		token: '',
		uerInfo: {},
		avatar: '',
		code: '',
		phoneModelId: '',
		phoneModelName: '',
		phoneNumber: '',
		phoneModelList: [],
		trolleyList: {
			invalidList: [],
			validList: []
		},
		addressList: [],
		defaultAddress: {},
		areaOptions: [],
		showPrivacy: false, //是否授权隐私协议
		messages: {},
		totalCredits: 0,
		isStore:'',
	},
	getters: {
		token(state) {
			return state.token
		},
		avatar(state) {
			return state.avatar
		},
		code(state) {
			return state.code
		},
		phoneModelId(state) {
			return state.phoneModelId
		},
		phoneModelName(state) {
			return state.phoneModelName
		},
		phoneNumber(state) {
			return state.phoneNumber
		},
		// 购物车列表
		trolleyList(state) {
			return state.trolleyList
		},
		// 购物车总商品数、总金额
		trolleySum(state) {
			let amount = 0
			let count = 0
			let selectCount = 0
			state.trolleyList.validList.forEach((item) => {
				count += Number(item.quantity)
				if (item.selected == true) {
					amount += Number(item.price) * Number(item.quantity)
					selectCount += Number(item.quantity)
				}
			})
			let sum = {
				productCount: selectCount,
				productAmount: amount / 100,
				allCount: count,
			}
			return sum
		},
		// 收货地址列表
		addressList(state) {
			return state.addressList
		},
		// 默认收货地址
		defaultAddress(state) {
			return state.defaultAddress
		},
		phoneModelList(state) {
			return state.phoneModelList
		},
		areaOptions(state) {
			return state.areaOptions
		},
		showPrivacy(state) {
			return state.showPrivacy
		},
		messages(state) {
			return state.messages
		},
		totalCredits(state) {
			return state.totalCredits
		},
		isStore(state) {
			return state.isStore
		},
	},
	mutations: {
		setTOken(state, token) {
			state.token = token
		},
		setUserInfo(state, userInfo) {
			state.userInfo = userInfo
			state.avatar = userInfo.avatar
			state.code = userInfo.code
			state.phoneModelId = userInfo.phoneModelId
			state.phoneModelName = userInfo.phoneModelName
			state.phoneNumber = userInfo.phoneNumber
		},
		setAvatar(state, avatar) {
			state.avatar = avatar
		},
		setCode(state, code) {
			state.code = code
		},
		setPhoneModelId(state, phoneModelId) {
			state.phoneModelId = phoneModelId
		},
		setPhoneModelName(state, phoneModelName) {
			state.phoneModelName = phoneModelName
		},
		setPhoneNumber(state, phoneNumber) {
			state.phoneNumber = phoneNumber
		},
		setTrolley(state, trolleyList) {
			state.trolleyList = trolleyList
		},
		setProduct(state, product) {

		},
		getAddressList(state, addressList) {
			state.addressList = addressList
			// 设置默认地址
			state.defaultAddress = addressList.filter(item => {
				return item.defaultAddress
			})[0] || {}
		},
		async setPhoneModelList(state, phoneModelList) {
			await new Promise((resolve) => {
				getphoneModel().then(res => {
					state.phoneModelList = res.data
					resolve()
				})
			})

		},
		setAreaOptions(state, areaOptions) {
			state.areaOptions = areaOptions
		},
		steShowPrivacy(state, data) {
			state.showPrivacy = data
		},
		setMessages(state, data) {
			state.messages = data
		},
		setTotalCredits(state, data) {
			state.totalCredits = data
		},
		
		setIsStore(state, data) {
			state.isStore = data
		},
	},
	actions: {
		getTrolley(context, data) {
			getTrolleyList().then(res => {
				context.commit('setTrolley', res.data)
			})
		},
		// 添加删除商品
		setProduct(context, {
			phoneCaseId,
			quantity
		}) {
			let postProdut = {
				phoneCaseId: phoneCaseId,
				quantity: quantity
			}
			addTrolley(postProdut).then(res => {
				uni.$u.debounce(() => {
					context.dispatch('getTrolley')
				})
			})
		},
		// 获取收货地址列表
		async getAddressList(context, data) {
			return new Promise((resolve) => {
				getAddressList({
					pageSize: 10
				}).then(res => {
					context.commit('getAddressList', res.data)
					resolve()
				})
			})

		},
		// 获取地域列表
		getAreaOptions(context, data) {
			if (context.state.areaOptions.length == 0) {
				getAreaOptions().then(res => {
					context.commit('setAreaOptions', res.data)
				})
			}

		},
		// 获取机型列表
		setPhoneModelList(context, data) {
			getphoneModel().then(res => {
				context.commit('setPhoneModelList', res.data)
			})
		}

	}
})
export default store